﻿//Copyright 2011 Andres Rutnik - used under license
// ===================================
using System;
using System.Windows;
using log4net;
using WPFEdit.Framework;

namespace SampleApp
{
    class EditorApp
    {

        static EditorApp s_instance = null;

		private SampleDocumentManager m_documentManager = new SampleDocumentManager();

        private Window m_window;

        private static ILog s_logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public EditorApp(Window mainWindow)
        {
            if (s_instance == null)
            {
                s_instance = this;
            }
            m_window = mainWindow;
        }

        public void Shutdown()
        {
            m_window.Close();
        }

        internal static EditorApp Instance
        {
            get { return EditorApp.s_instance; }
        }

        internal SampleDocumentManager DocumentManager
        {
            get { return m_documentManager; }
        }

        internal MainMenuModel MainMenu
        {
            get { return m_documentManager.MainMenu; }
        }

        internal void Init()
        {
            try
            {
                TaskManager taskMan = new TaskManager();
                Task initTask = new Task() { WindowTitle = "Initializing Application" };

                m_documentManager.AddInitSteps(initTask);

                if (!taskMan.RunSynchronousTask(false, initTask))
                {
                    this.Shutdown();
                }
            }
            catch (Exception e)
            {
                if (e.GetType() == typeof(TaskException)) e = ((TaskException)e).StepException;

                s_logger.Error(String.Format("Error on initialization : {0}.  {1}", e.Message, e.StackTrace));
				MessageBox.Show("Error on initialization" + e);
				this.Shutdown();
            }
        }
    }
}